package com.ruoyi.web.controller.train;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.Jobtitle;
import com.ruoyi.system.service.IJobtitleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 职称Controller
 *
 * @author dp
 * @date 2023-11-28
 */
@Api(tags = {"职称"})
@RestController
@RequestMapping("/dict/jobtitle")
public class JobtitleController extends BaseController {
    @Autowired
    private IJobtitleService jobtitleService;
    @Autowired
    TokenService tokenService;

    /**
     * 查询职称列表
     */
    @ApiOperation(value = "职称列表", notes = "查询职称列表详情", tags = {"职称管理"})
    //@PreAuthorize("@ss.hasPermi('system:jobtitle:list')")
    @GetMapping("/list")
    public TableDataInfo list(Jobtitle jobtitle) {
        startPage();
        List<Jobtitle> list = jobtitleService.selectJobtitleList(jobtitle);
        return getDataTable(list);
    }

    /**
     * 导出职称列表
     */
    @ApiOperation(value = "职称列表导出", notes = "导出职称列表", tags = {"职称管理"})
    //@PreAuthorize("@ss.hasPermi('system:jobtitle:export')")
    @Log(title = "职称", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Jobtitle jobtitle) {
        List<Jobtitle> list = jobtitleService.selectJobtitleList(jobtitle);
        ExcelUtil<Jobtitle> util = new ExcelUtil<Jobtitle>(Jobtitle.class);
        util.exportExcel(response, list, "职称数据");
    }

    /**
     * 获取职称详细信息
     */
    @ApiOperation(value = "获取职称详细信息", notes = "获取职称详细信息详情", tags = {"职称管理"})
    //@PreAuthorize("@ss.hasPermi('system:jobtitle:query')")
    @GetMapping(value = "/{jobtitleId}")
    public AjaxResult getInfo(@PathVariable("jobtitleId") Long jobtitleId) {
        return success(jobtitleService.selectJobtitleByJobtitleId(jobtitleId));
    }

    /**
     * 新增职称
     */
    @ApiOperation(value = "新增职称", notes = "新增职称详情", tags = {"职称管理"})
    //@PreAuthorize("@ss.hasPermi('system:jobtitle:add')")
    @Log(title = "职称", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(HttpServletRequest request, @RequestBody Jobtitle jobtitle) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        jobtitleService.completeData(jobtitle, userId, 1);
        return toAjax(jobtitleService.insertJobtitle(jobtitle));
    }

    /**
     * 修改职称
     */
    @ApiOperation(value = "修改职称", notes = "修改职称详情", tags = {"职称管理"})
    //@PreAuthorize("@ss.hasPermi('system:jobtitle:edit')")
    @Log(title = "职称", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(HttpServletRequest request,@RequestBody Jobtitle jobtitle) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        jobtitleService.completeData(jobtitle, userId, 2);
        return toAjax(jobtitleService.updateJobtitle(jobtitle));
    }

    /**
     * 删除职称
     */
    @ApiOperation(value = "删除职称", notes = "删除职称详情", tags = {"职称管理"})
    //@PreAuthorize("@ss.hasPermi('system:jobtitle:remove')")
    @Log(title = "职称", businessType = BusinessType.DELETE)
    @DeleteMapping("/{jobtitleIds}")
    public AjaxResult remove(@PathVariable Long[] jobtitleIds) {
        return toAjax(jobtitleService.deleteJobtitleByJobtitleIds(jobtitleIds));
    }
}
